package com.srwl.mytx.map;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;


/**
 * Created by dahai2070 on 2016/11/29 0029.
 */
public class GaodeLocationClient implements AMapLocationListener {
    private static final String TAG = "GaodeLocationClient";

    private AMapLocationClient mLocClient;
    private Context context;
    private LocationListener locationListener;
    private boolean needAddress;

    public GaodeLocationClient(Context context) {
        this.context = context;
        initLocation();
    }

    public GaodeLocationClient(Context context, boolean needAddress) {
        this.context = context;
        this.needAddress = needAddress;
        initLocation();
    }

    private void initLocation() {
        mLocClient = new AMapLocationClient(context);
        mLocClient.setLocationListener(this);
        AMapLocationClientOption mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(1000);
        mLocClient.setLocationOption(mLocationOption);
        mLocClient.startLocation();
    }

    public void locationOnce() {
        if (mLocClient != null) {
            mLocClient.startLocation();
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation == null) {
            Log.e(TAG, "onLocationChanged: 定位结果为空");
            return;
        }

        Log.i(TAG, "onLocationChanged: 位置改变，经纬度：" + aMapLocation.getLatitude() + "   " + aMapLocation.getLongitude() + "位置：" + aMapLocation.getAddress());

        if (aMapLocation.getLatitude() != 0.0 && aMapLocation.getLongitude() != 0.0) {
            if (needAddress) {
                if (!TextUtils.isEmpty(aMapLocation.getAddress())) {
                    if (locationListener != null) {
                        locationListener.locationSuccess(aMapLocation);
                    }
                    mLocClient.stopLocation();
                }
            } else {
                if (locationListener != null) {
                    locationListener.locationSuccess(aMapLocation);
                }
                mLocClient.stopLocation();
            }
        } else {
            Log.e("AmapError", "location Error, ErrCode:"
                    + aMapLocation.getErrorCode() + ", errInfo:"
                    + aMapLocation.getErrorInfo());
        }
    }


    public interface LocationListener {
        void locationSuccess(AMapLocation location);
    }

    public void setLocationListener(LocationListener locationListener) {
        this.locationListener = locationListener;
    }

    public void clear() {
        locationListener = null;
        mLocClient.onDestroy();
    }
}
